<!-- TODO -->
<template>
    <div>
        <!-- header -->
        <el-card class="!border-none" shadow="never">
            <el-alert type="warning" :closable="false" show-icon>
                <template #title>
                    1、审核用户申请的售后订单；2、申请售后需要用户确认收货之后才能申请，平台同意退款后处于等待退款的状态，平台会自动进行原路退款操作
                </template>
            </el-alert>

            <el-form ref="formRef" class="mb-[-16px] mt-[15px]" :inline="true" :model="queryParams">
                <el-form-item label="订单搜索" prop="name">
                    <el-select
                        placeholder="全部"
                        class="w-[240px] h-[32px]"
                        v-model="queryParams.keyword"
                    >
                        <el-option label="订单编号" value="order_sn" />
                        <el-option label="用户编号" value="user_sn" />
                        <el-option label="用户昵称" value="nickname" />
                        <el-option label="收件人名称" value="contact" />
                        <el-option label="收件人手机号" value="mobile" />
                        <el-option label="售后单号" value="after_sale_sn" />
                    </el-select>
                    <el-input
                        class="w-[240px] h-[32px]"
                        placeholder="请输入"
                        clearable
                        v-model="queryParams.keyword_value"
                    />
                </el-form-item>
                <el-form-item label="商品名称">
                    <el-input
                        class="w-[240px] h-[32px]"
                        placeholder="请输入"
                        clearable
                        v-model="queryParams.goods_name"
                    />
                </el-form-item>
                <el-form-item label="订单来源" prop="name">
                    <el-select
                        placeholder="全部"
                        class="w-[240px] h-[32px]"
                        v-model="queryParams.order_terminal"
                    >
                        <el-option label="全部" value="" />
                        <el-option label="小程序商城" :value="1" />
                        <el-option label="H5商城" :value="3" />
                        <el-option label="公众号商城" :value="2" />
                        <el-option label="苹果APP商城" :value="5" />
                        <el-option label="安卓APP商城" :value="6" />
                    </el-select>
                </el-form-item>
                <el-col>
                    <el-form-item label="申请时间" prop="name">
                        <daterange-picker
                            v-model:startTime="queryParams.start_time"
                            v-model:endTime="queryParams.end_time"
                        />
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="resetPage">查询</el-button>
                        <el-button @click="resetParams">重置</el-button>
                        <!-- <el-button @click="resetParams">导出</el-button> -->
                    </el-form-item>
                </el-col>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <el-tabs class="demo-tabs" v-model="queryParams.status" @tab-click="handleTabclick">
                <el-tab-pane :label="`全部(${pager.extend.all_num || 0})`" :name="0"></el-tab-pane>
                <el-tab-pane
                    :label="`申请中(${pager.extend.ing_num || 0})`"
                    :name="1"
                ></el-tab-pane>
                <el-tab-pane
                    :label="`等待退款(${pager.extend.wait_num || 0})`"
                    :name="2"
                ></el-tab-pane>
                <el-tab-pane
                    :label="`售后成功(${pager.extend.success_num || 0})`"
                    :name="3"
                ></el-tab-pane>
                <el-tab-pane
                    :label="`售后失败(${pager.extend.fail_num || 0})`"
                    :name="4"
                ></el-tab-pane>
                <el-table size="large" class="mt-[14px]" :data="pager.lists">
                    <el-table-column label="售后单号" prop="sn" min-width="180" />

                    <el-table-column label="买家昵称" prop="nickname" min-width="150">
                        <template #default="{ row }">
                            <el-popover placement="bottom-start" width="200px" trigger="hover">
                                <div class="flex">
                                    <span class="flex-none mr-[20px]">头像：</span>
                                    <el-image
                                        :src="row.user.avatar"
                                        style="width: 40px; height: 40px; border-radius: 50%"
                                    />
                                </div>
                                <div class="flex mt-[20px] col-top">
                                    <span class="flex-none mr-[20px]">昵称：</span>
                                    <span>{{ row.user.nickname }}</span>
                                </div>
                                <div class="flex mt-[20px] col-top">
                                    <span class="flex-none mr-[20px]">编号：</span>
                                    <span>{{ row.user.sn }}</span>
                                </div>
                                <template #reference>
                                    <div class="pointer normal">
                                        {{ row.nickname }}
                                    </div>
                                </template>
                            </el-popover>
                        </template>
                    </el-table-column>
                    <el-table-column label="订单编号" prop="order_sn" min-width="180" />
                    <el-table-column label="商品信息" min-width="280">
                        <template #default="{ row }">
                            <div class="mt-[6px] flex" style="height: 65px">
                                <el-image
                                    :src="getImageUrl(row.goods_image)"
                                    style="width: 58px; height: 58px"
                                    class="flex-none"
                                >
                                </el-image>

                                <el-tooltip :content="row.goods_name" placement="top-start">
                                    <div class="flex flex-col justify-between ml-2">
                                        <span class="ml-[3px]">{{ row.goods_name }}</span>
                                        <span class="ml-[3px]">￥{{ row.goods_price }}</span>
                                    </div>
                                </el-tooltip>
                            </div>
                        </template>
                    </el-table-column>

                    <el-table-column label="数量" prop="goods_num" min-width="120" />
                    <el-table-column label="实付金额" prop="total_pay_price" min-width="120" />
                    <el-table-column label="退款金额" prop="actual_refund_amount" min-width="120" />
                    <el-table-column label="退款方式" prop="refund_method_desc" min-width="90" />
                    <el-table-column label="售后状态" prop="status_desc" min-width="120">
                        <template #default="{ row }">
                            <span v-if="row.status == 0"> {{ row.status_desc }}</span>
                            <span v-if="row.status == 1" style="color: rgba(249, 134, 43, 1)">
                                {{ row.status_desc }}</span
                            >
                            <span v-if="row.status == 2" style="color: rgba(1, 203, 87, 1)">
                                {{ row.status_desc }}</span
                            >
                            <span v-if="row.status == 3" style="color: red">
                                {{ row.status_desc }}</span
                            >
                        </template>
                    </el-table-column>
                    <el-table-column label="申请时间" prop="create_time" min-width="120" />
                    <el-table-column label="操作" fixed="right">
                        <template #default="{ row }">
                            <el-button type="primary">
                                <router-link
                                    :to="{
                                        path: getRoutePath('after.after/detail'),
                                        query: {
                                            id: row.id
                                        }
                                    }"
                                >
                                    售后详情
                                </router-link>
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-tabs>

            <!-- 分页 -->
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>

<script lang="ts" setup>
import { usePaging } from '@/hooks/usePaging'
import { aftersaleLists } from '@/api/order'
import { getRoutePath } from '@/router'
import useAppStore from '@/stores/modules/app'
const { getImageUrl } = useAppStore()
const queryParams = reactive({
    keyword: 'order_sn',
    keyword_value: '',
    goods_name: '',
    order_terminal: '',
    start_time: '',
    end_time: '',
    status: 0
})
const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: aftersaleLists,
    params: queryParams
})
const handleTabclick = async () => {
    await nextTick()
    getLists()
}
getLists()
</script>
<style lang="scss" scope>
//TODO
.el-alert--warning.is-light {
    background-color: rgba(246, 248, 255, 1);
    color: rgba(64, 115, 250, 1);
}
</style>
